using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.ApplicationModel.Activation;
using Windows.Storage;
using Windows.Storage.Search;
using Windows.UI.Xaml;

namespace LFMTest
{
    partial class App
    {
        public App()
        {
            InitializeComponent();
        }

        protected async override void OnLaunched(LaunchActivatedEventArgs args)
        {
            Window.Current.Content = new Profile();
            Window.Current.Activate();
            musicLibrary = new ObservableCollection<MusicStorageFile>();
            isLibraryLoaded = false;
            await LoadMusicLibrary();
        }

        protected override void OnSearchActivated(Windows.ApplicationModel.Activation.SearchActivatedEventArgs args)
        {
            var searchResultsPage = new LFMTest.SearchResults();
            searchResultsPage.Activate(args);
        }

        public ObservableCollection<MusicStorageFile> musicLibrary { get; private set; }
        public Boolean isLibraryLoaded { get; private set; }


        public async Task LoadMusicLibrary()
        {
            QueryOptions msqo = new QueryOptions(CommonFileQuery.OrderByMusicInfo, new List<String> { ".mp3" });

            var filesList = await Windows.Storage.KnownFolders.MusicLibrary.CreateFileQueryWithOptions(msqo).GetFilesAsync();
            int cur = 0;

            foreach (var item in filesList)
            {
                try
                {
                    StorageFile file = item as StorageFile;
                    var p = await file.Properties.GetMusicPropertiesAsync();
                    MusicStorageFile musicFile = new MusicStorageFile
                    {
                        Title = p.Title,
                        Album = p.Album,
                        Artist = p.Artist,
                        Bitrate = p.Bitrate,
                        Duration = p.Duration,
                        Rating = p.Rating,
                        TrackNumber = p.TrackNumber,
                        File = file
                    };
                    musicLibrary.Add(musicFile);
                    cur++;
                }
                catch
                {
                    Debugger.Break();
                }
            }

            isLibraryLoaded = true;
        }
    }
}
